package com.zxx.coolweather.util;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.zxx.coolweather.db.City;
import com.zxx.coolweather.db.County;
import com.zxx.coolweather.db.Province;
import com.zxx.coolweather.gson.Weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author zxx on 2021/8/25
 */
public class Utility {

    //http://guolin.tech/api/china/
    public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvince = new JSONArray(response);
                for (int i = 0; i < allProvince.length(); i++) {
                    JSONObject proObj = allProvince.getJSONObject(i);
                    Province province = new Province();
                    province.provinceName = proObj.optString("name");
                    province.provinceCode = proObj.optInt("id");
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Province province = new Gson().fromJson(response, Province.class);
        }
        return false;
    }

    //http://guolin.tech/api/china/16
    public static boolean handleCityResponse(String response, int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCity = new JSONArray(response);
                for (int i = 0; i < allCity.length(); i++) {
                    JSONObject cityObj = allCity.getJSONObject(i);
                    City city = new City();
                    city.cityName = cityObj.optString("name");
                    city.cityCode = cityObj.optInt("id");
                    city.provinceId = provinceId;
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    //http://guolin.tech/api/china/16/113
    public static boolean handleCountyResponse(String response, int cityId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounty = new JSONArray(response);
                for (int i = 0; i < allCounty.length(); i++) {
                    JSONObject countyObj = allCounty.getJSONObject(i);
                    County county = new County();
                    county.countyName = countyObj.optString("name");
                    county.countyCode = countyObj.optInt("id");
                    county.weatherId = countyObj.optString("weather_id");
                    county.cityId = cityId;
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //http://guolin.tech/api/weather?cityid=CN101190401&key=bc0418b57b2d4918819d3974ac1285d9
    public static Weather handleWeatherResponse(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
            String weatherContent = jsonArray.getJSONObject(0).toString();

            return new Gson().fromJson(weatherContent, Weather.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
